/**
 * 树转数组
 */
 export function treeToList(
    tree = [],
    idValue = null,
    childrenField = 'children',
    idField = 'id',
    parentIdField = 'parentId'
  ) {
    const list = []
    if (!childrenField) childrenField = 'children'
    for (let i = 0, j = tree.length; i < j; i++) {
      const d = tree[i]
      const id = d[idField]
      if (!list.some(l => l[idField] === id)) {
        list.push(d)
      }
      if (parentIdField) d[parentIdField] = idValue
      const children = d[childrenField]
      if (children && children.length > 0) {
        const items = treeToList(
          children,
          id,
          childrenField,
          idField,
          parentIdField
        )
        const values = items.values()
        for (const v of values) {
          if (!list.some(l => l[idField] === v[idField])) {
            list.push(v)
          }
        }
      }
    }
    return list
  }
  
  /**
   * 数组转树
   */
   export function listToTree(
    list = [],
    root = null,
    idField = 'id',
    parentIdField = 'parentId'
  ) {
    const tree = []
    const hash = {}
    const childrenField = 'children'
    for (let i = 0, l = list.length; i < l; i++) {
      const d = list[i]
      hash[d[idField]] = d
    }
  
    for (let i = 0, l = list.length; i < l; i++) {
      const d = list[i]
      const parentID = d[parentIdField]
      if (
        parentID === '' ||
        parentID === 0 ||
        parentID === null ||
        parentID === undefined
      ) {
        tree.push(d)
        continue
      }
  
      const parent = hash[parentID]
      if (!parent) {
        tree.push(d)
        continue
      }
  
      let children = parent[childrenField]
      if (!children) {
        children = []
        parent[childrenField] = children
      }
      children.push(d)
    }
  
    if (root) {
      root[childrenField] = tree
      return [root]
    }
  
    return tree
  }
  



// 排序的树转数组和数组转树
// 树转数组
export function treeToList(
  tree = [],
  idValue = null,
  childrenField = 'children',
  idField = 'id',
  parentIdField = 'parentId',
  orderField = 'order' // 添加一个字段记录原始顺序
) {
  const list = []
  if (!childrenField) childrenField = 'children'
  for (let i = 0, j = tree.length; i < j; i++) {
    const d = tree[i]
    const id = d[idField]
    if (!list.some(l => l[idField] === id)) {
      // 添加原始顺序信息
      d[orderField] = i
      list.push(d)
    }
    if (parentIdField) d[parentIdField] = idValue
    const children = d[childrenField]
    if (children && children.length > 0) {
      const items = treeToList(
        children,
        id,
        childrenField,
        idField,
        parentIdField,
        orderField
      )
      const values = items.values()
      for (const v of values) {
        if (!list.some(l => l[idField] === v[idField])) {
          // 添加原始顺序信息
          v[orderField] = i
          list.push(v)
        }
      }
    }
  }
  return list
}

// 数组转树
export function listToTree(
  list = [],
  root = null,
  idField = 'id',
  parentIdField = 'parentId',
  orderField = 'order' // 添加一个字段记录原始顺序
) {
  const tree = []
  const hash = {}
  const childrenField = 'children'
  for (let i = 0, l = list.length; i < l; i++) {
    const d = list[i]
    hash[d[idField]] = d
  }

  for (let i = 0, l = list.length; i < l; i++) {
    const d = list[i]
    const parentID = d[parentIdField]
    if (
      parentID === '' ||
      parentID === 0 ||
      parentID === null ||
      parentID === undefined
    ) {
      tree.push(d)
      continue
    }

    const parent = hash[parentID]
    if (!parent) {
      tree.push(d)
      continue
    }

    let children = parent[childrenField]
    if (!children) {
      children = []
      parent[childrenField] = children
    }
    children.push(d)
  }

  if (root) {
    root[childrenField] = tree.sort((a, b) => a[orderField] - b[orderField]) // 按原始顺序排序
    return [root]
  }

  return tree.sort((a, b) => a[orderField] - b[orderField]) // 按原始顺序排序
}
